﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

/**
 * SpawnPoint class.
 * A SpawnPoint that contains a position
 * to spawn at, and flags for what-to-spawn
 * and whether-we-should-spawn.
 */
public class SpawnPoint
{
    // Member vars here
    private Vector3 m_position;
    private Vector2 m_topLeft;
    private Vector2 m_bottomRight;
    private bool m_isActive;
    private ObjectFactory.TypeEnum m_typeToSpawn;

    /**
     * SpawnPoint's constructor.
     * @param position
     * @param type
     */
    public SpawnPoint(Vector3 position, CollidableObject.Type type)
    {
        m_position = position;
        m_isActive = true;
        m_typeToSpawn = type.myType;
        Vector3 screenOrigin = Camera.worldToScreen(new Vector3(0, 0, 0));
        Vector3 relativeScreenPos = Camera.worldToScreen(m_position);
        Vector2 flatPos = new Vector2(relativeScreenPos.X-screenOrigin.X, relativeScreenPos.Y-screenOrigin.Y);
        m_topLeft = flatPos + type.cullMinimumOffset;
        m_bottomRight = flatPos + type.cullMaximumOffset;
    }

    /**
     * SpawnPoint constructor used to create SpawnPoints from data read from file
     */
    public SpawnPoint(Vector3 position, Vector2 topLeft, Vector2 bottomRight, bool isActive, ObjectFactory.TypeEnum type)
    {
        m_position = position;
        m_topLeft = topLeft;
        m_bottomRight = bottomRight;
        m_isActive = isActive;
        m_typeToSpawn = type;
    }

    /**
     * Test whether this SpawnPoint is active.
     * Active implies that we will spawn an
     * instance of our type once we are within
     * spawn bounds.
     */
    public bool isActive() { return m_isActive; }

    /**
     * Resets this SpawnPoint.
     */
    public void reset()
    {
        m_isActive = true;
    }

    /**
     * Calls the ObjectFactory to spawn this.
     */
    public CollidableObject spawn()
    {
        m_isActive = false;
        return ObjectFactory.get(this);
    }

    /**
     * Not explaining this one :/ .
     * Fine! Position in worldCoords.
     */
    public Vector3 getPosition()
    {
        return m_position;
    }

    public Vector2 getTopLeft()
    {
        return m_topLeft;
    }

    public Vector2 getBottomRight()
    {
        return m_bottomRight;
    }

    /**
     * Get the type we are meant to spawn.
     */
    public ObjectFactory.TypeEnum getType()
    {
        return m_typeToSpawn;
    }

    /**
     * Returns the bounds of this SP.
     */
    public Rectangle getWorldBounds()
    {
        return new Rectangle((int)m_topLeft.X, (int)m_topLeft.Y, (int)(m_bottomRight.X - m_topLeft.X), (int)(m_bottomRight.Y - m_topLeft.Y));
    }
}
